import request from '@/utils/request'

// 定义接口类型
interface UserInfo {
  id: number;
  username: string;
  nickname: string;
  avatar: string;
  roles: string[];
  [key: string]: any;
}

interface UserListParams {
  page?: number;
  pageSize?: number;
  username?: string;
  nickname?: string;
  mobile?: string;
  status?: number;
}

// 登录
export function login(data: any) {
  return request({
    url: '/base/login',
    method: 'post',
    data
  })
}

// 获取当前登录用户信息
export function getInfo(): Promise<{ data: UserInfo }> {
  return request({
    url: '/user/info',
    method: 'post'  // 改为post方式，与后端一致
  })
}

// 登出
export function logout() {
  return request({
    url: '/base/logout',
    method: 'post'
  })
}

// 获取用户列表
export function getUsers(params: UserListParams): Promise<{ data: { list: UserInfo[], total: number } }> {
  return request({
    url: '/user/list',
    method: 'get',
    params
  })
}

// 更新用户登录密码
export function changePwd(data: { oldPassword: string, newPassword: string }): Promise<any> {
  return request({
    url: '/user/changePwd',
    method: 'post',
    data
  })
}

// 创建新用户
export function createUser(data: Partial<UserInfo>): Promise<any> {
  return request({
    url: '/user/create',
    method: 'post',
    data
  })
}

// 根据ID更新用户信息
export function updateUserById(id: number, data: Partial<UserInfo>): Promise<any> {
  return request({
    url: `/user/update/${id}`,
    method: 'put',
    data
  })
}

// 批量删除用户
export function batchDeleteUserByIds(ids: number[]): Promise<any> {
  return request({
    url: '/user/batchDelete',
    method: 'delete',
    data: { ids }
  })
} 